package com.hqd.jjvm.instructions;

import org.apache.commons.lang.ArrayUtils;

public enum InstructionClassifyType {
	CONST(0, 20), LOAD(21, 53),
	STORE(54, 86), STACK(87, 95), MATH(96, 132), CHANGE(133, 147),
	COMPARE(148, 166), CONTROL(167, 177), REF(178, 195), EXTEND(196, 201), RESERVE(202, 255);

	private int start;
	private int end;
	private int[] supplement;

	InstructionClassifyType(int start, int end, int... supplement) {
		this.start = start;
		this.end = end;
		this.supplement = supplement;
	}

	public static InstructionClassifyType getType(Integer code) {
		InstructionClassifyType[] classifyTypes = InstructionClassifyType.values();
		for (InstructionClassifyType classifyType : classifyTypes) {
			if (code >= classifyType.start && code <= classifyType.end) {
				return classifyType;
			}
			if (ArrayUtils.isNotEmpty(classifyType.supplement)) {
				for (int i = 0; i < classifyType.supplement.length; i++) {
					if (classifyType.supplement[i] == code) {
						return classifyType;
					}
				}
			}
		}
		return null;
	}
}
